package com.zky.controller;

import com.zky.entity.EmergencyInfo;
import com.zky.service.EmergencyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/emergency")
public class EmergencyInfoController {
    
    @Autowired
    private EmergencyInfoService emergencyInfoService;
    
    @GetMapping("/list")
    public ResponseEntity<List<EmergencyInfo>> getAllEmergencyInfo() {
        try {
            List<EmergencyInfo> list = emergencyInfoService.getAllEmergencyInfo();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    
    @PostMapping
    public ResponseEntity<?> createEmergencyInfo(@RequestBody EmergencyInfo emergencyInfo) {
        try {
            // 打印接收到的数据
            System.out.println("Received emergency info: " + emergencyInfo);
            
            // 创建信息
            emergencyInfoService.createEmergencyInfo(emergencyInfo);
            
            // 打印创建后的数据
            System.out.println("Created emergency info: " + emergencyInfo);
            
            return ResponseEntity.ok().body(Map.of(
                "message", "创建成功",
                "data", emergencyInfo
            ));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "创建失败: " + e.getMessage()
            ));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<?> updateEmergencyInfo(
            @PathVariable Integer id,
            @RequestBody EmergencyInfo emergencyInfo) {
        try {
            emergencyInfo.setId(id);
            emergencyInfoService.updateEmergencyInfo(emergencyInfo);
            return ResponseEntity.ok().body(Map.of(
                "message", "更新成功",
                "data", emergencyInfo
            ));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "更新失败: " + e.getMessage()
            ));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteEmergencyInfo(@PathVariable Integer id) {
        try {
            emergencyInfoService.deleteEmergencyInfo(id);
            return ResponseEntity.ok().body(Map.of(
                "message", "删除成功"
            ));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "message", "删除失败: " + e.getMessage()
            ));
        }
    }
} 